// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004-2009 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using Microsoft.Win32;
using System;
using System.Globalization;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Collections;
using System.Windows.Forms;

namespace SoapTrace.Core.ApplicationFramework
{
	/// <summary>
	/// Remoting server so that an application's instances can communicate
	/// with each other.
	/// </summary>
	public class SingleInstanceServer 
	{
		/// <summary>
		/// Application context.  Used to forward command lines to the 
		/// application from other instances.
		/// </summary>
		private static ISingleInstanceApplicationContext m_Context;

		/// <summary>
		/// Creates a new instance of the communicator class.
		/// </summary>
		public SingleInstanceServer( ISingleInstanceApplicationContext context )
		{
			m_Context = context;
		}

		/// <summary>
		/// Register the server.  After registration server requests will be
		/// forwarded onto the application context.
		/// </summary>
		public void Register( )
		{
			// Register remoting object type.

			FirstInstance FirstInstance = new FirstInstance( );
             
			string Uri = String.Format( CultureInfo.InvariantCulture, 
			    "{0}.Remoting",
				m_Context.GetType( ).FullName );
			
			RemotingServices.Marshal( FirstInstance, Uri );

			// Register remoting channel.

			IDictionary ChannelProperties = new Hashtable( 2 );
			ChannelProperties[ "bindTo" ] = "127.0.0.1";
            ChannelProperties[ "port" ] = 0; // pick a random open port

			TcpChannel Channel = new TcpChannel( ChannelProperties, null, null );
			ChannelServices.RegisterChannel( Channel, false );

			string Url = Channel.GetUrlsForUri( Uri )[ 0 ];

			// Store url so clients can access the server.
			using( RegistryKey Key = Application.UserAppDataRegistry ) 
			{
				Key.SetValue( m_Context.GetType( ).FullName, Url );
			}
		}

		/// <summary>
		/// Called by the FirstInstance remotable object.  This allows 
		/// a second instance to pass its command line arguments to the 
		/// first instance for processing.
		/// </summary>
		/// <param name="args">Command line arguments.</param>
		/// <returns></returns>
		public static int RunSecondInstance( string[ ] args )
		{
			int ReturnCode = 0;

			ReturnCode = m_Context.RunSecondInstance( args );

			return ReturnCode;
		}
	}
}
